define(['app'], function(app) {
    'use strict';
    var controller = function controller($scope, Newproductpost_give, $stateParams, GoDialog) {
     
        //获取参数
        $scope.act_id = $stateParams.id;
         //加载数据
        $scope.data_init = function(){
        	Newproductpost_give.detail($scope.act_id, function (data) {
          	  $scope.detail = data;
       	    });
       	    Newproductpost_give.goodslist($scope.act_id, function (data) {
          	  $scope.goodslist = data;
       	    });
        }
         $scope.data_init();
      
        $scope.isHide = true; //默认隐藏
        //隐藏弹窗
        $scope.hide_apply = function () {
                $scope.isHide = true;
            }
        //显示弹窗
        $scope.show_apply = function (value) {
            $scope.isHide = false;
        }
        
        //选择答案--单选,以questions.id对CheckBox进行分组
        $scope.getAnswer = function(index,id){
        	var answerCheckbox = document.getElementsByClassName("answerCheckbox"+id);
        	//console.log("答案个数="+answerCheckbox.length+" 下标="+index+" 问题id="+id+" 值="+answerCheckbox[index].value);
        	for (var i=0;i<answerCheckbox.length;i++) {
        		answerCheckbox[i].checked = "";
        	}
        	answerCheckbox[index].checked = "checked";
        }
        
        //提交问卷
        $scope.submit = function(){
        	var answerAll = document.getElementsByClassName("answerAll");//逐个判断哪个是选中状态
        	var answer = new Array();
        	//console.log(answerAll.length+"问题的个数="+$scope.detail.paper.questions.length);
        	var xb = 0;//数组下标
        	for (var i=0;i<answerAll.length;i++) {
        		if(answerAll[i].checked){
        			answer[xb] = answerAll[i].value;
        			xb++;
        		}
        	}
        	//console.log(answer);
        	if(answer.length!=$scope.detail.paper.questions.length){//如果答案数组的长度不等于问题的个数
        		GoDialog.toast("请回答所有问题！");
        	}else{
    			$scope.paper_id = $scope.detail.paper.paper_id;//问卷id
	        	Newproductpost_give.apply($scope.act_id, $scope.paper_id, answer, function (data) {
	        		if(data.success){
	        			GoDialog.toast("提交数据成功！");
	        		}else{
	        			GoDialog.toast("提交数据失败！");
	        		}
	     	    });
        	}
        }
        
    }
    controller.$inject = ['$scope', 'Newproductpost_give', '$stateParams', 'GoDialog'];
    return app.registerController("newproductpost_givectrl",controller);
});